﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SN.Models;
using SN.Data;
namespace SN.Report
{
    public partial class WarningFormualDlg : Form
    {

        public FormTemp FrmTemp;

        private r_Zbzd zbzd;

        public r_Zbzd Zbzd
        {
            get { return zbzd; }
            set { zbzd = value; }
        }

        public WarningFormualDlg()
        {
            InitializeComponent();
        }

        private void WarningFormualDlg_Load(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            richTextBox1.Text = "";


            lbCell.Text = zbzd.Zbzd_Zbbh;
            r_Zbyj obj = DBUtility.DB.From<r_Zbyj>().Where(r_Zbyj._.Zbyj_ReportID == zbzd.Zbzd_ReportID &&
                                                           r_Zbyj._.Zbyj_Zbbh == zbzd.Zbzd_Zbbh).ToSingle();
            if (obj != null)
            {
                richTextBox1.Text = obj.Zbyj_Yjtj;
                comboBox1.Text = obj.Zbyj_Yjlb == 1 ? "单元格背景" : "单元格字体";
                textBox1.Text = obj.Zbyj_Yjz1;
                textBox2.Text = obj.Zbyj_Yjz2;

            }
            if (!string.IsNullOrEmpty(textBox1.Text))
            {
                if (comboBox1.Text == "单元格背景")
                    textBox1.BackColor = ParseColor(Convert.ToInt32(textBox1.Text));
                else
                    textBox1.ForeColor = ParseColor(Convert.ToInt32(textBox1.Text));
            }

            if (!string.IsNullOrEmpty(textBox2.Text))
            {
                if (comboBox1.Text == "单元格背景")
                    textBox2.BackColor = ParseColor(Convert.ToInt32(textBox2.Text));
                else
                    textBox2.ForeColor = ParseColor(Convert.ToInt32(textBox2.Text));
            }

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            r_Zbyj objYj = DBUtility.DB.From<r_Zbyj>().Where(r_Zbyj._.Zbyj_ReportID == zbzd.Zbzd_ReportID &&
                                                                   r_Zbyj._.Zbyj_Zbbh == zbzd.Zbzd_Zbbh).ToSingle();
            if (objYj == null)
            {
                objYj = new r_Zbyj();
                objYj.Zbyj_ReportID = zbzd.Zbzd_ReportID;
                objYj.Zbyj_Zbbh = zbzd.Zbzd_Zbbh;
                objYj.Zbyj_Yjtj = richTextBox1.Text.Trim(); //预警条件
                objYj.Zbyj_Yjlb = comboBox1.Text == "单元格背景" ? 1 : 2; //预警类别 
                objYj.Zbyj_Yjz1 = textBox1.Text;
                objYj.Zbyj_Yjz2 = textBox2.Text;

                objYj.Detach();
            }
            else
            {
                objYj.Zbyj_Yjtj = richTextBox1.Text.Trim(); //预警条件
                objYj.Zbyj_Yjlb = comboBox1.Text == "单元格背景" ? 1 : 2; //预警类别 
                objYj.Zbyj_Yjz1 = textBox1.Text;
                objYj.Zbyj_Yjz2 = textBox2.Text;

                objYj.Attach();
            }

            if (string.IsNullOrEmpty(objYj.Zbyj_Yjz1))
            {
                MessageBox.Show("预警结果不能为空!");
                return;
            }

            int i = DBUtility.DB.Save<r_Zbyj>(objYj);
            if (i > 0)
            {
                MessageBox.Show("保存成功!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

        }
        //Color 转int
        private uint ParseRGB(Color color)
        {
            return (uint)(((uint)color.B << 16) | (ushort)(((ushort)color.G << 8) | color.R));
        }
        // int 转 Color
        private Color ParseColor(int color)
        {
            int red = color & 255;
            int green = color >> 8 & 255;
            int blue = color >> 16 & 255;
            return Color.FromArgb(red, green, blue);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            ColorDialog loColorForm = new ColorDialog();
            if (loColorForm.ShowDialog() == DialogResult.OK)
            {

                textBox1.Text = ((int)ParseRGB(loColorForm.Color)).ToString();
                if (comboBox1.Text == "单元格背景")
                    textBox1.BackColor = ParseColor(Convert.ToInt32(textBox1.Text));
                else
                    textBox1.ForeColor = ParseColor(Convert.ToInt32(textBox1.Text));
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += "[Calc]" + lbCell.Text;
        }

        private void btnGreater_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += btnGreater.Text;
        }

        private void btnLess_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += btnLess.Text;
        }

        private void btnEquals_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += btnEquals.Text;
        }

        private void btnNotequals_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += btnNotequals.Text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ColorDialog loColorForm = new ColorDialog();
            if (loColorForm.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = ((int)ParseRGB(loColorForm.Color)).ToString();
                if (comboBox1.Text == "单元格背景")
                    textBox2.BackColor = ParseColor(Convert.ToInt32(textBox2.Text));
                else
                    textBox2.ForeColor = ParseColor(Convert.ToInt32(textBox2.Text));
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //预警判断
            string sTemp = richTextBox1.Text;
            while (sTemp.IndexOf("[Calc]") >= 0)
            {
                int i = sTemp.IndexOf("[Calc]");
                if (i >= 0)
                {
                    string sText = sTemp.Substring(i, 12);
                    //获取指标编号
                    string sZbbh = sText.Replace("[Calc]", "");
                    //取值（从后台)
                    string sValue = "10";
                    //替换指标值
                    sTemp = sTemp.Replace(sText, sValue);
                }
            }
            object result = SN.Services.Expression.ComputeExpression(sTemp);
            MessageBox.Show(result.ToString());
        }
    }
}
